package com.dk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import com.dk.entity.*;
import com.dk.mapper.DtPayLogMapper;
import io.jsonwebtoken.Claims;
import com.dk.mapper.DtGoodsMapper;
import com.dk.mapper.DtOrderMapper;
import com.dk.mapper.TbAddressMapper;
import com.dk.service.OrderServiceI;
import com.netflix.zuul.context.RequestContext;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderServiceI {

    @Autowired
    private DtGoodsMapper dtGoodsMapper;//商品

    @Autowired
    private TbAddressMapper tbAddressMapper; //地址

    @Autowired
    private DtOrderMapper dtOrderMapper;

    @Autowired
    private DtPayLogMapper dtPayLogMapper;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public DtGoods selectByGoodsId(Long goodsId) {
        return dtGoodsMapper.selectById(goodsId);
    }

    @Override
    public List<TbAddress> selectAddressByUserId(String id) {
        QueryWrapper<TbAddress> tbAddressQueryWrapper = new QueryWrapper<>();
        QueryWrapper<TbAddress> eq = tbAddressQueryWrapper.eq("user_id", Long.valueOf(id)).eq("is_default", 1);
        List<TbAddress> tbAddresses = tbAddressMapper.selectList(eq);
        return tbAddresses;
    }
//生成订单表
    @Override
    public void insertOrder(DtOrder dtOrder) {
        dtOrderMapper.insert(dtOrder);
    }
//修改状态
    @Override
    public void updatestatus(String out_trade_no, String trade_no) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("paymentorderno" , out_trade_no);
        DtOrder dtOrder = new DtOrder();
        dtOrder.setRunningwater(trade_no);
        dtOrder.setStatus("2");
        dtOrderMapper.update(dtOrder,queryWrapper);
    }
//增加支付日志
    @Override
    public void insertPayLog(String out_trade_no, String total_amount, String trade_no , String id) {
        QueryWrapper<DtOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("paymentorderno", out_trade_no);
        DtOrder dtOrder = dtOrderMapper.selectOne(wrapper);
        DtPayLog dtPayLog = new DtPayLog();
        dtPayLog.setOutTradeNo(out_trade_no);
        dtPayLog.setCreateTime(new Date());
        dtPayLog.setPayTime(new Date());
        dtPayLog.setUserId(id);
        dtPayLog.setTransactionId(trade_no);
        dtPayLog.setOrderList(dtOrder.toString());
        dtPayLog.setTradeState("1");
        dtPayLog.setPayType("1");
        dtPayLogMapper.insert(dtPayLog);
    }

    @Override
    public DtOrder selectOrderId(Long orderId) {
        DtOrder dtOrder = dtOrderMapper.selectById(orderId);
        return dtOrder;
    }

    //    增加评论
    @Override
    public void insertEvaluate(Evaluate evaluate) {
        mongoTemplate.insert(evaluate);
    }
}
